package types

import (
	"database/sql/driver"
	"strings"
)

type StringArray []string

func (p *StringArray) Scan(src interface{}) error {
	var source string
	switch src.(type) {
	case string:
		source = src.(string)
	case []byte:
		source = string(src.([]byte))
	}
	if len(strings.Trim(source, " ")) == 0 {
		*p = []string{}
		return nil
	}
	*p = strings.Split(source, ",")
	return nil
}

func (p StringArray) Value() (driver.Value, error) {
	v := ""
	if p != nil {
		for k, v := range p {
			p[k] = strings.ReplaceAll(v, ",", "，")
		}
		v = strings.Join(p, ",")
	}
	return driver.Value(v), nil
}
